package com.app.android.coliy.ui.activity;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.app.android.coliy.R;
import com.app.android.coliy.ui.view.Comm_EditView;
import com.app.android.coliy.ui.view.Comm_SubmitBtnView;
import com.app.android.coliy.ui.view.MultiStateView;
import com.app.android.coliy.util.ToastUtil;
import com.feasycom.bean.BluetoothDeviceWrapper;
import com.feasycom.controler.FscBleCentralApi;
import com.feasycom.controler.FscBleCentralApiImp;
import com.feasycom.controler.FscBleCentralCallbacksImp;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2018/6/20.
 */

public class SendDataActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        deviceDetail = (BluetoothDeviceWrapper) getIntent().getSerializableExtra("device");
        setContentView(R.layout.activity_senddata);
        initView();
        initBlue();
    }

    MultiStateView multiplestatusView;
    LinearLayout ll_content;
    TextView tv_cancel, tv_progress;
    Comm_EditView et_edittext;
    Comm_SubmitBtnView csb_ok;

    BluetoothDeviceWrapper deviceDetail;
    FscBleCentralApi fscBleCentralApi = null;

    //初始化控件
    private void initView() {
        setTitle("发送数据");    //设置标题
        setLeftImgClickListener();  //设置左键返回

        ll_content = (LinearLayout) findViewById(R.id.ll_content);
        tv_cancel = findViewById(R.id.tv_cancel);
        et_edittext = findViewById(R.id.et_edittext);
        csb_ok = findViewById(R.id.csb_ok);
        tv_progress = findViewById(R.id.tv_progress);

        ll_content.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        csb_ok.setOnClickListener(this);
    }

    private void initBlue() {
        fscBleCentralApi = FscBleCentralApiImp.getInstance(mContext);
        fscBleCentralApi.setCallbacks(new FscBleCentralCallbacksImp() {
            @Override
            public void characteristicForService(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic characteristic) {
//                currentWriteCharacteristic = characteristic;
            }

            @Override
            public void servicesFound(BluetoothGatt gatt, BluetoothDevice device, ArrayList<BluetoothGattService> services) {
//                serviceList = services;
            }

            @Override
            public void blePeripheralConnected(BluetoothGatt gatt, BluetoothDevice device) {
//                uiHandlerDeviceConnected();
//                if (AUTH_TEST) {
//                    Log.i(TAG,"count "+Integer.valueOf(AUTH_TEST_COUNT++).toString());
//                    handler.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            disconnectDevice();
//                            activity.finish();
//                        }
//                    }, 8000);
//                }
                Log.i("test", "连接成功");
            }

            @Override
            public void blePeripheralDisonnected(BluetoothGatt gatt, BluetoothDevice device) {
//                uiHandlerDeviceDisconnected();
            }

            @Override
            public void packetReceived(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String strValue, String hexString, byte[] rawValue, String timestamp) {
//                uiHandlerReceiveData(rawValue, strValue, hexString);
            }

            @Override
            public void sendPacketProgress(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattCharacteristic ch, int percentage, byte[] tempByte) {
                Log.i("test", "发送回调："+ percentage);
            }
        });

        connect();
    }

    private void connect() {
        fscBleCentralApi.connect(deviceDetail.getAddress());
    }

    private void sendData(byte[] currentPackge) {
        fscBleCentralApi.send(currentPackge);
    }

    private void send() {
        if (TextUtils.isEmpty(et_edittext.getText().toString())) {
            ToastUtil.showWarning(mContext, "请输入内容");
            return;
        }
        try {
            byte[] currentPackge = et_edittext.getText().toString().getBytes("UTF-8");
            sendData(currentPackge);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    //点击事件
    @Override
    public void onClick(View view) {
        super.onClick(view);
        Intent intent;
        switch (view.getId()) {
            case R.id.csb_ok:
                send();
                break;

        }
    }
}
